<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-4-14
 * Time: 下午1:49
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report extends Modules_Controller
{

    function __construct()
    {
        parent::__construct();

        $this->rules = array(
            "rule" => array(
                array(
                    "field" => "title",
                    "label" => lang('title'),
                    "rules" => "trim|required|min_length[1]"
                )
            ,array(
                    "field" => "timeline",
                    "label" => lang('time'),
                    "rules" => "trim|strtotime"
                )
            ,array(
                    "field" => "content",
                    "label" => lang('conent'),
                    "rules" => "trim"
                    // link_create tag 生成
                )
            ,array(
                    "field" => "photo",
                    "label" => lang('photo'),
                    "rules" => "trim"
                )
            )
        );

    }

    public function _edit_data()
    {
        $form=$this->input->post();
        $form['timeline'] = time();
        return $form;
    }
    public function copypro()
    {
        $ids = $this->input->post('ids');

        $rs=$this->model->get_one($ids);

        unset($rs['id']);
        unset($rs['sort_id']);
        unset($rs['timeline']);

        $id = $this->model->create($rs);
        if ($id) {
            $vdata['msg'] = '复制成功，请刷新查看';
            $vdata['status'] = 1;
        }else{
            $vdata['msg'] = '复制失败，请刷新后重试';
            $vdata['status'] = 0;
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }
    public function index($cid=false,$page=1)
    {
        // 栏目路径
        $vdata['cpath']= $this->mcol->get_path_more($this->cid);
        $vdata['cchildren'] = $this->mcol->get_cols($this->cid);
        $title = $this->mcol->get_one($this->cid,"title");
        $vdata['title'] = $title['title'];

        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

        $order = $this->_index_orders();
        if ($this->input->get('order',TRUE)) {
            // TODO: order
            // $orders = explode("-",$this->input->get('order',TRUE));
            $order = $this->input->get('order',TRUE);
        }
        $where_in = array("report.cid"=>$this->cid,"report.ccid"=>$this->ccid);
        // 条件必须
        $where = array_merge($this->_index_where(),$where_in);
        $count = $this->model->get_count_all($where,false,array('account','account.id=report.rid','left'));
        $vdata['pages'] = $this->_pages_join(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$count,4);
        //$vdata['pages'] = $this->_pages(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$where,4);
        $fields = 'report.*,account.uname as runame,account.phone as rphone';
        $vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where,$fields,false,array('account','account.id=report.rid','left'));
        if($vdata['list']) {
            foreach($vdata['list'] as &$row) {
                $account = $this->macc->get_one($row['uid'],'uname,phone');
                $row['uname'] = $account ? $account['uname'] : '';
                $row['phone'] = $account ? $account['phone'] : '';
            }
        }
        $total = $this->model->get_status_nums();
        $vdata['total'] = $total['total'];

        $this->_display($vdata);
    }
    protected function _index_where(){
        $arr =array();
        if (isset($_GET['ctype']) && $_GET['ctype']) {
            $arr['report.ctype'] = $_GET['ctype'];
        }
        if (isset($_GET['uname']) && $_GET['uname']) {
            $arr['like'] = array('account.uname',$_GET['uname']);
            //$arr['account.old_com_name'] = $_GET['old_com_name'];
        }
        return $arr;
    }
    public function one() {
        $data = $this->input->get();
        $res = $this->model->set($data['id'],array('status'=>$data['status']));
        if($res) {
            redirect(base64_decode($data['ref']));
        }
    }
}
